﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace WP8Demos.ViewModels
{
    public class NotifyPropertyChangedViewModel : INotifyPropertyChanged
    {
        private readonly DispatcherTimer timer;
        private readonly List<string> examples;
        private int examplesCounter;
        private string textExample = "Hello";
        public event PropertyChangedEventHandler PropertyChanged;


        public NotifyPropertyChangedViewModel()
        {
            examples = new List<string>() { "Hello", "It's about time", "To kick some bottom !" };
            examplesCounter = 0;

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += ModifyTextExample;
            timer.Start();
        }

        public string TextExample
        {
            get
            {
                return textExample;
            }
            set
            {
                if (this.textExample != value)
                {
                    textExample = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private void ModifyTextExample(object sender, EventArgs e)
        {
            TextExample = examples[examplesCounter];
            examplesCounter++;
            if (examplesCounter == 3)
            {
                examplesCounter = 0;
            }
        }

        private void NotifyPropertyChanged([CallerMemberName]string propertyName = "")
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
